var widthofwindow = $(window).width();
widthofwindow = widthofwindow-960;
windowsize = widthofwindow/2;

$(document).ready(function(){
if($("#addarmy").is("input")){
	$("#finishbutton").hide();
	var map = $("#mapname").val();
	var russia = 4, italy = 3, germany = 3, france = 3, austria = 3, england = 3, turkey = 3;
	var landarray = new Array(russia,italy,germany,france,austria,england,turkey);
	var namearray = new Array('russia','italy','germany','france','austria','england','turkey');
	var colorarray = new Array('white','green','black','lightblue','red','darkblue','yellow');
	var landcount = 0;
	var arraycount = 0;
	//$("#addarmy").attr('value', "Add army for " + namearray[arraycount]);
	$("#addarmy").click(function(){
		if(arraycount == 6 && landcount == 3){
			$("#addarmy").unbind("click");
			$("#finishbutton").show();
		}
		if(namearray[arraycount] == 'russia'){
			if(landcount == 4){
				landcount = 0;
				arraycount++;
			}
		}else{
			if(landcount == 3){
				landcount = 0;
				arraycount++;
			}
		}
		Army();
		landcount++;
	});
}

function Army(){
	var move = true;
	var armyimg = $("<img src='images/" + colorarray[arraycount] + "army.png' class='army'/>");
	armyimg.appendTo("#options");
	//console.debug($("#map"));
	$(document).mousemove(function(e){
		if(move == true){
			armyimg.css('left', e.pageX).css('top', e.pageY);
		}
	});
	armyimg.click(function(e){
		move = false;
		var X = e.pageX - windowsize + 10;
		var Y = e.pageY - 160;
		$.post('controller/findprovince.php', {'x' : X, 'y' : Y, 'map' : map},
			function(whatprovince){
				var unitid = namearray[arraycount].substring(0,3) + landcount;
				var province = whatprovince;
				var color = colorarray[arraycount];
				var land = namearray[arraycount];
				var data = {"unitid" : unitid, "province" : province, "color" : color, "map" : map, 'land' : land};
				$.post('controller/addunits.php', {'armyinfo' : data, 'unitid' : unitid, 'map' : map}
				, function(data){
					console.debug(data);
				}
				);
			}
		);
	});
}
});